package com.jianzhile.jobs.activities;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.gugalor.citylist.CityList;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.fragments.AddFragment;
import com.jianzhile.jobs.fragments.ExplorerFragment;
import com.jianzhile.jobs.fragments.HomeFragment;
import com.jianzhile.jobs.fragments.MineFragment;
import com.jianzhile.jobs.fragments.WorkFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wutairui on 16/3/2.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener,WorkFragment.OnFragmentInteractionListener, HomeFragment.OnFragmentInteractionListener,  MineFragment.OnFragmentInteractionListener, ExplorerFragment.OnFragmentInteractionListener, AddFragment.OnFragmentInteractionListener {


    public final String TAG = "MainActivity";

    @Bind(R.id.activity_main_home_ib)
    public ImageButton homeIb;

    @Bind(R.id.activity_main_work_ib)
    public ImageButton workIb;

    @Bind(R.id.activity_main_add_ib)
    public ImageButton addIb;
    @Bind(R.id.activity_main_explore_ib)
    public ImageButton exploreIb;

    @Bind(R.id.activity_main_mine_ib)
    public ImageButton mineIb;

    @Bind(R.id.action_bar_left_ib)
    public TextView abLeftIb;

    @Bind(R.id.action_bar_right_btn)
    public ImageButton abRightIb;


    @Bind(R.id.action_bar_title)
    public TextView abTitleTv;


    private FragmentManager fm;
    private HomeFragment homeFragment;
    private WorkFragment workFragment;
    private ExplorerFragment explorerFragment;
    private MineFragment mineFragment;
    private AddFragment addFragment;

    public LocationClient mLocationClinet = null;
    public BDLocationListener myListener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        hideFragments();

        if (homeFragment == null) {
            homeFragment = HomeFragment.newInstance(null, null);
            fm = getFragmentManager();
            fm.beginTransaction().add(R.id.activity_main_container, homeFragment).commit();

        }

        initClick();
        myListener = new MyLocationListener();
        mLocationClinet = new LocationClient(getApplicationContext());
        mLocationClinet.registerLocationListener(myListener);
        initLocation();
        mLocationClinet.start();
        mLocationClinet.requestLocation();
    }

    private void initLocation() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        int span = 1000;
        option.setScanSpan(0);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClinet.setLocOption(option);
    }

    private void initClick() {
        homeIb.setOnClickListener(this);
        workIb.setOnClickListener(this);
        exploreIb.setOnClickListener(this);
        mineIb.setOnClickListener(this);
        abRightIb.setOnClickListener(this);
        abLeftIb.setOnClickListener(this);
        addIb.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        FragmentTransaction ft = fm.beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

        switch (v.getId()) {

            case R.id.activity_main_home_ib:
                hideFragments();
                ft.show(homeFragment);
                initToolBar(0);
                break;
            case R.id.activity_main_work_ib:
                hideFragments();
                if (workFragment == null) {
                    workFragment = WorkFragment.newInstance(null, null);
                    ft.add(R.id.activity_main_container, workFragment);
                } else {
                    ft.show(workFragment);
                }
                initToolBar(1);
                break;
            case R.id.activity_main_add_ib:
                hideFragments();
                if (addFragment == null) {
                    addFragment = AddFragment.newInstance(null, null);
                    ft.add(R.id.activity_main_container, addFragment);
                } else {
                    ft.show(addFragment);
                }
                initToolBar(2);
                break;
            case R.id.activity_main_explore_ib:
                hideFragments();
                if (explorerFragment == null) {
                    explorerFragment = ExplorerFragment.newInstance(null, null);
                    ft.add(R.id.activity_main_container, explorerFragment);
                } else {
                    ft.show(explorerFragment);
                }
                initToolBar(3);
                break;
            case R.id.activity_main_mine_ib:
                hideFragments();
                if (mineFragment == null) {
                    mineFragment = MineFragment.newInstance(null, null);
                    ft.add(R.id.activity_main_container, mineFragment);
                } else {
                    ft.show(mineFragment);
                }
                initToolBar(4);
                break;
            case R.id.action_bar_right_btn:

                Intent intent = new Intent(MainActivity.this, RegistActivity.class);
                startActivity(intent);
                break;
            case R.id.action_bar_left_ib:
                Intent addressIntent = new Intent(MainActivity.this, CityList.class);
                startActivityForResult(addressIntent, 1);
                break;
        }
        ft.commit();
    }


    private void hideFragments() {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (homeFragment != null)
            ft.hide(homeFragment);
        if (workFragment != null)
            ft.hide(workFragment);
        if (explorerFragment != null)
            ft.hide(explorerFragment);
        if (mineFragment != null)
            ft.hide(mineFragment);
        if (addFragment != null)
            ft.hide(addFragment);
        ft.commit();
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    public static int tab = 0;

    public void initToolBar(int tab) {

        this.tab = tab;
        switch (tab) {
            case 0:
                abTitleTv.setText(getString(R.string.app_name));
                abLeftIb.setVisibility(View.VISIBLE);
                abRightIb.setVisibility(View.VISIBLE);
                abRightIb.setImageResource(R.drawable.ic_flip_black_36dp);
                break;
            case 1:
                abTitleTv.setText("兼职");
                abLeftIb.setVisibility(View.GONE);
                abRightIb.setVisibility(View.VISIBLE);
                abRightIb.setImageResource(R.drawable.ic_flip_black_36dp);
                break;
            case 2:
                abTitleTv.setText("发布兼职");
                abLeftIb.setVisibility(View.GONE);
                abRightIb.setVisibility(View.GONE);
                break;
            case 3:
                abTitleTv.setText("发现");
                abLeftIb.setVisibility(View.GONE);
                abRightIb.setVisibility(View.GONE);
                break;
            case 4:
                abTitleTv.setText("我");
                abLeftIb.setVisibility(View.GONE);
                abRightIb.setVisibility(View.VISIBLE);
                abRightIb.setImageResource(R.drawable.ic_settings_blue_grey_200_36dp);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            switch (resultCode) {
                case 2:
                    abLeftIb.setText(" " + data.getStringExtra("city"));
                    break;

                default:

                    break;
            }
        }
    }


    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location.getCity() != null) {
                abLeftIb.setText(" " + location.getCity());
            } else {
                abLeftIb.setText(" 北京");
            }

        }
    }

    public void chooseJobByKeyWord(int type) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        if (workFragment == null) {
            workFragment = WorkFragment.newInstance(null, null);
            ft.add(R.id.activity_main_container, workFragment);
        } else {
            ft.show(workFragment);
        }
        ft.commit();
        //workFragment.chooseByKeyWord(type);
    }

}
